<template>
    <div class="btn-type">
        <!-- <topUp ref="topUp"></topUp> -->
        <el-dropdown trigger="click">
            <span class="el-dropdown-link">
                <el-button type="primary" icon="el-icon-arrow-up" circle size="mini"
                    style="transform:rotate(225deg)  scale(0.6);border: 0;border-radius: 14px;"></el-button>
                <span>
                    <span>分析记录:</span>
                    <span style="font-weight: 700;">{{ dataList.ongoingCount || 0 }}</span>
                </span>
            </span>
            <el-dropdown-menu slot="dropdown" style="padding:0 15px;width: 300px;min-height: 300px;">
                <div id="content_dropdown_Records">
                    <div style="margin:15px 0;cursor: pointer;" class="operation">
                        <div class="title">
                            <div>分析记录（进行中）</div>
                            <!-- <el-tooltip class="item" effect="dark" content="加入VIP,提升运行数量！" placement="top">
                                <i class="iconfont icon-dkw_tisheng" @click="topUp"></i>
                            </el-tooltip> -->
                            <div class="limit">
                                <span>运行上限:</span>
                                <span>{{ dataList.maxRunCount }}</span>
                            </div>
                        </div>

                        <template v-if="ongoingList.operation.length">
                            <div class="operation_content" v-for="item in ongoingList.operation">
                                <template v-if="item.status">
                                    <div class="operation_content_task" @dblclick="addRemarks(item)">
                                        <el-tooltip class="item" effect="dark" placement="left">
                                            <div slot="content">{{ item.menuName + '/' + item.directory }}<br />备注：{{
                                                item.remark
                                                ||
                                                '暂无备注信息' }}</div>
                                            <div>{{ item.content }}</div>
                                        </el-tooltip>
                                    </div>
                                    <div class="operation_content_tool">
                                        <i class="el-icon-delete" @click="delRun(item.taskId)"></i>
                                    </div>
                                </template>
                                <template v-else>
                                    <div class="operation_content_task">
                                        <div class="remark_item">
                                            <el-input placeholder="请输入备注！" v-model="remark" clearable>
                                            </el-input>
                                            <el-button type="primary" size="mini"
                                                @click="confirmRemarks(item, 'run')">确认</el-button>
                                        </div>
                                    </div>
                                </template>
                            </div>
                        </template>
                        <template v-else>
                            <div class="operation_content"
                                style="text-align: center;background-color: #fff;font-size: 14px;font-family: 'Kaiti';font-weight: 700;">
                                <div class="operation_content_task">
                                    暂无进行中分析记录
                                </div>
                            </div>
                        </template>
                    </div>
                    <div style="margin:15px 0;cursor: pointer;" class="queue">
                        <div class="title">
                            <div>分析记录（等待中）</div>
                            <!-- <el-tooltip class="item" effect="dark" content="加入VIP,提升队列数量！" placement="top">
                                <i class="iconfont icon-dkw_tisheng" @click="topUp"></i>
                            </el-tooltip> -->
                            <div class="limit">
                                <span>队列上限:</span>
                                <span>{{ dataList.maxWaitingCount }}</span>
                            </div>
                        </div>
                        <template v-if="ongoingList.queue.length">
                            <div class="queue_content" v-for="item in ongoingList.queue">
                                <template v-if="item.status">
                                    <div class="operation_content_task" @dblclick="addRemarks(item)">
                                        <el-tooltip class="item" effect="dark" placement="left">
                                            <div slot="content">{{ item.menuName + '/' + item.directory }}<br />备注：{{
                                                item.remark ||
                                                '暂无备注信息' }}</div>
                                            <div>{{ item.content }}</div>
                                        </el-tooltip>
                                    </div>
                                    <div class="operation_content_tool">
                                        <i class="iconfont icon-xiangshang" @click="changePosition(item.content)"></i>
                                        <i class="el-icon-delete" @click="delTask(item.content)"></i>
                                    </div>
                                </template>
                                <template v-else>
                                    <div class="operation_content_task">
                                        <div class="remark_item">
                                            <el-input placeholder="请输入备注！" v-model="remark" clearable>
                                            </el-input>
                                            <el-button type="primary" size="mini"
                                                @click="confirmRemarks(item, 'task')">确认</el-button>
                                        </div>
                                    </div>
                                </template>
                            </div>
                        </template>
                        <template v-else>
                            <div class="operation_content"
                                style="text-align: center;background-color: #fff;font-size: 14px;font-family: 'Kaiti';font-weight: 700;">
                                <div class="operation_content_task">
                                    暂无等待中分析记录
                                </div>
                            </div>
                        </template>
                    </div>
                </div>
            </el-dropdown-menu>
        </el-dropdown>
        <!-- 
        <el-dropdown trigger="click">
            <span class="el-dropdown-link">
                <el-button type="primary" icon="el-icon-arrow-up" circle size="mini"
                    style="transform:rotate(225deg)  scale(0.6);border: 0;border-radius: 14px;"></el-button>
                <span>
                    <span>分析进行中:</span>
                    <span style="font-weight: 700;">{{ dataList.ongoingCount || 0 }}</span>
                </span>
            </span>
            <el-dropdown-menu slot="dropdown" style="padding:0 15px;width: 300px;min-height: 300px; max-height: 600px !important;
        overflow-y: auto !important;">
                <div id="content_dropdown_menu">
                    <div style="margin:15px 0;cursor: pointer;">分析记录（进行中）</div>
                    <collapse-vue :dataList="dataList.ongoing" v-on="$listeners" v-bind="$attrs" :status="'ongoing'" />
                </div>
            </el-dropdown-menu>
        </el-dropdown> -->

        <el-dropdown trigger="click">
            <span class="el-dropdown-link">
                <el-button type="success" icon="el-icon-check" circle size="mini"
                    style="transform:scale(0.6);border: 0;border-radius: 14px;"></el-button>
                <span>
                    <span>分析成功:</span>
                    <span style="font-weight: 700;">{{ dataList.successfulCount || 0 }}</span>
                </span>
            </span>
            <el-dropdown-menu slot="dropdown" style="padding:0 15px;width: 300px;min-height: 300px; max-height: 600px !important;
        overflow-y: auto !important;">
                <div id="content_dropdown_menu">
                    <div style="margin:15px 0;">分析记录（成功）</div>
                    <collapse-vue :dataList="dataList.successful" v-on="$listeners" v-bind="$attrs"
                        :status="'successful'" />
                </div>
            </el-dropdown-menu>
        </el-dropdown>

        <el-dropdown trigger="click">
            <span class="el-dropdown-link">
                <el-button type="danger" icon="el-icon-close" circle size="mini"
                    style="transform: scale(0.6);border: 0;border-radius: 14px;"></el-button>
                <span>
                    <span>分析失败:</span>
                    <span style="font-weight: 700;">{{ dataList.failureCount || 0 }}</span>
                </span>
            </span>
            <el-dropdown-menu slot="dropdown" style="padding:0 15px;width: 300px;min-height: 300px; max-height: 600px !important;
        overflow-y: auto !important;">
                <div id="content_dropdown_menu">
                    <div style="margin:15px 0;">分析记录（失败）</div>
                    <collapse-vue :dataList="dataList.failure" v-on="$listeners" v-bind="$attrs" :status="'failureCount'" />
                </div>

            </el-dropdown-menu>
        </el-dropdown>

        <el-tooltip class="item" effect="dark" content="结果目录" placement="top">
            <span class="result_content" @click="$skipRouter('individual', 'taskManagement')"></span>
            <!-- <el-button plain size="mini" icon="el-icon-document"></el-button> -->
        </el-tooltip>

        <!-- <div class="btn_primary">
            <el-button type="primary" size="small">生成报告</el-button>
        </div> -->
    </div>
</template>

<script>
import collapseVue from './collapseVue.vue';
import { getToken } from '@/utils/auth';
import { getDrawTaskRecordRequest, deleteWaitingTaskRequest, upWaitingTaskTaskRequest, modifyTaskRemarkRequest, deleteRunTaskRequest } from '@/api/tools'
export default {
    name: 'BaiaoweifanUiUserIndex',
    components: {
        collapseVue
    },
    data() {
        return {
            ws: null,
            clickName: '',
            dataList: {},
            ongoingList: {
                operation: [],
                queue: [],
            },
            remark: '',
            flag: true,
        };
    },

    async created() {
        this.getList()
        if (getToken()) {
            this.ws = await this.$webSocket('home')
            this.ws.onopen = () => {
                console.log('打开连接');
            },
                this.ws.onmessage = (evt) => {
                    if (JSON.parse(evt.data).drawTaskList) {
                        const list = JSON.parse(evt.data).drawTaskList
                        if(list[0].menuCode === this.$router.history.current.name){
                            this.$emit('drawTaskListChange', list)
                        }
                    }
                    if (JSON.parse(evt.data).drawTaskRecord) {
                        this.dataList = JSON.parse(evt.data).drawTaskRecord
                        this.ongoingList = JSON.parse(evt.data).drawTaskRecord.ongoing
                        this.$forceUpdate()
                        this.flag = true
                    }
                    if (JSON.parse(evt.data).home_session_id) {
                        console.log(JSON.parse(evt.data).home_session_id);
                        localStorage.setItem('homeSessionId', JSON.parse(evt.data).home_session_id)
                    }
                },
                this.ws.onclose = () => {
                    console.log('断开');
                },
                this.ws.onerror = (value) => {
                    // console.log(value, '错误！');
                }
        }
    },
    beforeDestroy() {
        //断开webSocket
        if (this.ws) {
            this.ws.close()
        }
    },
    methods: {
        async getList() {
            const res = await getDrawTaskRecordRequest()
            if (res.code !== 200) return this.$message.error(res.message)
            this.dataList = res.data
            this.ongoingList = res.data.ongoing
            this.$forceUpdate();
        },
        topUp() {
            this.$refs.topUp.open()
        },
        //删除任务
        async delTask(taskName) {
            this.$confirm('该操作将清除此条任务记录，是否继续？', '提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
                beforeClose: async (action, instance, done) => {
                    if (action === 'confirm') {
                        const sessionId = localStorage.getItem('homeSessionId')
                        const res = await deleteWaitingTaskRequest({ sessionId, taskName })
                        if (res.code !== 200) {
                            this.$message.error(res.message)
                        } else {
                            this.$message({ type: 'success', message: res.message })
                        }
                    } else if (action === 'cancel') {
                        this.$message({
                            message: '取消操作',
                        })
                    }
                    done();
                }
            }).catch(() => { })
        },
        //删除运行中任务
        async delRun(taskId) {
            this.$confirm('该操作将清除此条任务记录，是否继续？', '提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
                beforeClose: async (action, instance, done) => {
                    if (action === 'confirm') {
                        const sessionId = localStorage.getItem('homeSessionId')
                        const res = await deleteRunTaskRequest({ sessionId, taskId })
                        if (res.code !== 200) {
                            this.$message.error(res.message)
                        } else {
                            this.$message({ type: 'success', message: res.message })
                        }
                    } else if (action === 'cancel') {
                        this.$message({
                            message: '取消操作',
                        })
                    }
                    done();
                }
            }).catch(() => { })
        },
        //改变任务位置
        async changePosition(taskName) {
            const sessionId = localStorage.getItem('homeSessionId')
            const res = await upWaitingTaskTaskRequest({ sessionId, taskName })
            if (res.code !== 200) return this.$message.error(res.message)
            this.$message({ type: 'success', message: res.message })
        },
        //添加备注
        addRemarks(item) {
            if (this.flag) {
                this.flag = false
                item.status = !item.status
                this.remark = item.remark
            }
        },
        //确认添加
        async confirmRemarks(item, taskType) {
            const sessionId = localStorage.getItem('homeSessionId')
            let remark = this.remark
            let taskName = item.content
            const res = await modifyTaskRemarkRequest({ sessionId, remark, taskName, taskType })
            if (res.code !== 200) {
                this.$message.error(res.message)
            }
            this.$message({ type: 'success', message: res.message })
            item.status = !item.status
            this.flag = true
        }

    },
};
</script>

<style lang="scss" scoped>
@import "~@/styles/variables.scss";

.btn-type {
    display: flex;
    align-items: center;
    min-width: 500px;

    ::v-deep .el-button {
        margin-right: 10px;
    }

    ::v-deep .el-dropdown-link {
        margin-right: 25px;
    }

    #content_dropdown_menu {
        width: 300px !important;
        min-height: 300px;

    }

    ::v-deep .el-dropdown {
        cursor: pointer;
    }

    .btn_primary>.el-button--primary {
        background: $themeTopToolColor;
        border: 0;
        border-radius: 15px;
    }

    .result_content {
        display: inline-block;
        background: url(../../../assets/icon_proview.png) no-repeat;
        cursor: pointer;
        width: 30px;
        height: 30px;
        margin: 0 30px 0;
    }
}

#content_dropdown_Records {
    display: Flex;
    flex-direction: column;

    .operation,
    .queue {
        max-height: 300px;
        overflow-y: auto;

        .title {
            display: flex;
            align-items: center;
            margin-bottom: 5px;


            i {
                width: 1em;
                // height: 1em;
                color: red;
                // font-size: 15px;
            }

            .limit {
                font-size: 11px;
                color: green;
                font-family: 'Kaiti';
                font-weight: 700;
            }
        }
    }

    .operation_content,
    .queue_content {
        padding: 5px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        background-color: #f1f1f1;
        border-radius: 4px;
        margin-bottom: 6px;

        .operation_content_task {
            flex: 1;
            overflow: hidden;
            text-wrap: nowrap;
            text-overflow: ellipsis;
            height: 25px;
            line-height: 25px;

            .remark_item {
                width: 100%;
                height: 25px;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }

            // display:flex;
            // justify-content:space-between;
            // font-size: 12px;
            ::v-deep .el-input {
                height: 25px;

                .el-input__inner {
                    height: 25px !important;
                    line-height: 25px !important;
                    border-radius: 3px;
                }

                .el-input__icon {
                    line-height: 25px;
                }
            }
        }

        .operation_content_tool {
            width: 80px;
            display: flex;
            justify-content: space-evenly;

            .icon-paixu,
            .el-icon-delete,
            .icon-xiangshang {
                display: inline-block;
                width: 25px;
                height: 25px;
                text-align: center;
                line-height: 25px;

                &:hover {
                    // background-color: #e0e0e0;
                    transform: scale(1.2);
                }
            }

            .el-icon-delete {
                color: red;
            }

            .icon-xiangshang {
                color: $themeTopToolColor;
            }
        }
    }

    .queue_content {
        max-height: 350px;
    }
}
</style>

